package com.luoran.bartalks.action;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.entity.biz.UserInfo;
import com.luoran.bartalks.service.IUserService;

/**
 * 用户相关的控制器
 * 
 * @author lifetime
 *
 */
@Controller
@RequestMapping("/my")
public class UserAction {
	@Autowired
	private IUserService userService;

	@RequestMapping("/info")
	public String info(Model model) {
		UserInfo info = userService.getUser(UserContext.get().getId());
		model.addAttribute("info", info);
		return "/biz/user/info";
	}

	@RequestMapping("/modifyPwd")
	public String modifyPwd(Model model, @RequestParam Map<String, String> params) {
		String number = params.get("login_no");
		String pwdOld = params.get("login_pwd_old");
		String pwdNew = params.get("login_pwd_new");
		if (StringUtils.isEmpty(number) || StringUtils.isEmpty(pwdOld) || StringUtils.isEmpty(pwdNew)) {
			model.addAttribute("title", "密码修改失败提醒！");
			model.addAttribute("msg", "账号、原始密码、新密码 不能为空！");
			return "/biz/msg-error";
		}
		if (!UserContext.get().getNumber().equals(number)) {
			model.addAttribute("title", "密码修改失败提醒！");
			model.addAttribute("msg", "高风险操作！您不能修改他人的账号密码");
			return "/biz/msg-error";
		}
		if (!userService.pwdSuccess(number, pwdOld)) {
			model.addAttribute("title", "密码修改失败提醒！");
			model.addAttribute("msg", "账号不存在或原始密码错误。");
			return "/biz/msg-error";
		} else {
			userService.modifyPwd(number, pwdNew);
			model.addAttribute("title", "密码修成功提醒！");
			model.addAttribute("msg", "密码修改成功。");
			return "/biz/msg-success";
		}

	}
}
